/**
* ...
* @author Default
* @version 0.1
*/

package  {
	
	import flash.display.*;
	import flash.geom.*;
	import flash.filters.*;
	import flash.utils.*;
	

	public class Scene3D extends Sprite
	{
		
		static public var focalLength:Number;
		
		static public var origin:Vertex;
		
		static public var camera:Camera3D;
		
		static public var canvas:Shape;
		
		
		public var visibleTriangles:int;
		public var vertices:Array;
		public var triangles:Array;
		
		public var W:Number;
		public var H:Number;
		public var D:Number;
		
		public var angleY:Number = 0;
		
		
		
		public function Scene3D( W:int = 1000, H:int = 1000, nb:int = 20, FL:Number = 1 ) 
		{
			
			this.W = W;
			this.H = H;
			this.D = H;
			
			origin = new Vertex( 0,0,0, 0xFFFF0000 );
			
			canvas = new Shape();
			addChild( canvas );
			
			focalLength = FL;
			
			var v0:Vertex;
			var v1:Vertex;
			var v2:Vertex;
			
			
			camera = new Camera3D( 0, 0, 0 );
			camera.viewport = new Rectangle( 0,0, 300,300 );
			
			
			var colors:Array = [ 0x993366, 0xFF66CC, 0x9999CC, 0xFF3399 ];
			var model:Model = new Model();
			triangles = [];
			triangles = model.triangles;
			
			
		}
		
		
		public function render():void
		{
			
			
			var i:int;
			var VL:int;			var TL:int = triangles.length;
			
			var v0:Vertex;
			var v1:Vertex;
			var v2:Vertex;
			
			var ox:Number;
			var oy:Number;
			
			var T:Triangle 

			var zmin:Number;
			var zmax:Number;
			var zindex:Number;
			
			visibleTriangles = 0;
			
			triangles.sortOn( 'DEPTH', Array.DESCENDING | Array.NUMERIC );
			zmax = triangles[ triangles.length - 1 ].DEPTH; 
			zmin = triangles[ 0 ].DEPTH;
			
			
			canvas.graphics.clear();
			
			angleY += Math.PI / 180;
			
			focalLength = 10000000000000000;
			focalLength = 10 + ( stage.stageHeight - mouseY)  *10
			
			for( var j:int = 0; j < TL; j++ )
			{
				
				T = triangles[ j ];
				
				T.move( stage );
				T.update();
				
				
				if( T.visible ) 
				{
					
					zindex = map( T.DEPTH, zmin, zmax, 0, 1 );
					
					if( T.DEPTH < camera.z + focalLength )
					{
						visibleTriangles++;
					
						VL = T.vertices.length;
						for ( i = 0; i < VL; i+=3 )
						{
							
							v0 = T.vertices[ i ];
							v1 = T.vertices[ i+1 ];
							v2 = T.vertices[ i+2 ];
							
							ox = v0.screenX;
							oy = v0.screenY;
							
							canvas.graphics.beginFill( T.color, T.alpha )
							canvas.graphics.moveTo( ox, oy );
							canvas.graphics.lineTo( v1.screenX , v1.screenY );
							canvas.graphics.lineTo( v2.screenX , v2.screenY );
							canvas.graphics.lineTo( ox, oy );
							canvas.graphics.endFill();
							
						}
					}
				}
			}
		}
		
		
		/*
		// -- cast x,y to [0, 1]
		a = x / w;
		b = y / h;
		
		// -- cast a,b to [-1, 1]
		//a = (a-.5)*2;
		//b = (b-.5)*2;
		
		// -- cast a,b to [1, 0, 1]
		//a = Math.abs((a-.5)*2);
		//b = Math.abs((b-.5)*2);
		
		// -- cast a,b to [0, 1 ,0]
		//a = Math.abs( Math.abs((a-.5)*2)-1);
		b = Math.abs( Math.abs((b-.5)*2)-1);
		*/
		
		/*
		 * 
		 * borner en tre min et max avec retour entre les valeurs
		 
			var min = 90;
			var max = 100;
			var xval = 101;

			var sign = xval / xval;
			var mod = xval % (max-min);
			var res = max - mod * sign;
			trace(mod, res);
			
			
		 * 
		*/
		public function mapBack( val:Number, min:Number, max:Number ):Number
		{
			return max - ( ( val % ( max - min ) ) * ( val / val ) );
		}
		
		public static function normalize(value:Number, minimum:Number, maximum:Number):Number 
		{
			return (value - minimum) / (maximum - minimum);
		}
		
		public static function interpolate(normValue:Number, minimum:Number, maximum:Number):Number 
		{
			return minimum + (maximum - minimum) * normValue;
		}
		
		public static function map(value:Number, min1:Number, max1:Number, min2:Number, max2:Number):Number 
		{
			return interpolate( normalize(value, min1, max1), min2, max2);
		}
		
	}
	
}
